﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace InterfaceComponents
{
    /// <summary>
    /// This represents a Healthbar in our HUD.
    /// </summary>
    public class Healthbar : InterfaceComponent, HUDComponent
    {
        private Texture2D healthTexture
        {
            get;
            set;
        }

        private Rectangle border
        {
            get;
            set;
        }

        private int value
        {
            get;
            set;
        }

        public int maxHealth
        {
            private get;
            set;
        }

        private Rectangle currentHealth
        {
            get;
            set;
        }

        private Texture2D backroundTexture;

        public Healthbar(Rectangle border, Texture2D tex, int val, Vector2 position, SpriteBatch spriteBatch, int maxHealth)
        {
            this.border = border;
            this.healthTexture = tex;
            this.value = val;
            this.position = position;
            this.spriteBatch = spriteBatch;
            this.maxHealth = maxHealth;
            //create black backgroundtexture
            backroundTexture = new Texture2D(spriteBatch.GraphicsDevice, 1, 1, false, SurfaceFormat.Color);
            backroundTexture.SetData(new[]{Color.Black});
        }

        /// <summary>
        /// Draws the Healthbar with the specified color.
        /// </summary>
        /// <param name="color">The color to tint the healthtexture.</param>
        public override void Draw(Color color)
        {
            spriteBatch.Draw(backroundTexture, border, color);
            float percentage = (float)value/(float)maxHealth;
            //check if the bealthbar grows sideward or upward
            if (border.Width > border.Height)
            {
                currentHealth = new Rectangle((int)position.X, (int)position.Y, (int)(percentage * border.Width), border.Height);
            }
            else
            {
                currentHealth = new Rectangle((int)position.X, (int)(position.Y + (1 - percentage) * border.Height), border.Width, (int)(border.Height - (1 - percentage) * border.Height));
            }
            spriteBatch.Draw(healthTexture, currentHealth, color);
        }

        /// <summary>
        /// SetValue is used by the HUDWrapper to update th component.
        /// </summary>
        public virtual void SetValue(Object value)
        {
            if (value.GetType() == typeof(int))
            {
                this.value = (int)value;
            }
        }
    }
}

